package com.nanshenycd.service.impl;

import com.nanshenycd.dao.TagDao;
import com.nanshenycd.pojo.Tag;
import com.nanshenycd.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TagServiceImpl implements TagService {
    @Autowired
    private TagDao tagDao;

    @Override
    public int saveTag(Tag tag) {
        return tagDao.saveTag(tag);
    }


    @Override
    public Tag getTagById(Long id) {
        return tagDao.getTagById(id);
    }

    @Override
    public Tag getTagByName(String name) {
        return tagDao.getTagByName(name);
    }

    @Override
    public List<Tag> listTag() {
        return tagDao.listTag();
    }

    @Override
    public List<Tag> listTagByIds(String ids) {
        List<Tag> tags =new ArrayList<>();
        List<Long> longs = convertToList(ids);
        for (Long aLong : longs) {
            tags.add(tagDao.getTagById(aLong));
        }
        return tags;
    }
    //拿到前端传过来的字符串进行解析成数组，最后添加到list集合中
    public List<Long> convertToList(String ids) {
        List<Long> list = new ArrayList<>();
        if (!"".equals(ids) && ids != null) {
            String[] idarray =ids.split(",");
            for(int i=0 ;i<idarray.length;i++){
                list.add(new Long(idarray[i]));
            }
        }
        return list;
    }


    public List<Tag> getAllTag(){
        return tagDao.getAllTag();
    }

    @Override
    public int updateTag(Tag tag) {
        return tagDao.updateTag(tag);
    }

    @Override
    public int deleteTag(Long id) {
        return tagDao.deleteTag(id);
    }

}
